/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim</a>, 2005
 */
package hk.com.carfield.action;

import hk.com.carfield.content.Content;
import hk.com.carfield.content.Editable;
import hk.com.carfield.core.ContentManager;
import hk.com.carfield.core.Context;
import hk.com.carfield.core.ItemManager;
import hk.com.carfield.item.Directory;
import hk.com.carfield.item.FileItem;
import hk.com.carfield.item.HeadItem;
import hk.com.carfield.item.Item;
import hk.com.carfield.util.TemplateUtil;
import hk.com.carfield.util.UrlUtil;
import hk.com.carfield.web.Request;

import java.io.File;
import java.io.IOException;
import java.util.SortedSet;

public final class Edit implements Body {
	private class EditDirectory extends Directory {
		private final SortedSet<Item> set = ItemManager.getSortedSet();

		public EditDirectory(final Context c, final File f, final Request req)
				throws IOException {
			super(c, f, req, 0);
			final File[] childs = location.listFiles(c.config.displayFilter());
			for (int i = 0; i < childs.length; i++) {
				if (childs[i].isFile()) {
					set.add(new EditItem(context, childs[i]));
				} else {
					set.add(new Directory(context, childs[i], req, 0));
				}
			}
			head.append(c.config.headerFileName + ":<textarea name=\""
					+ c.config.headerFileName + "\">"
					+ c.config.getHeader(location) + "</textarea><hr/>");
			foot.insert(0, "<hr/>" + c.config.readmeFileName
					+ ":<textarea name=\"" + c.config.readmeFileName + "\">"
					+ c.config.getReadme(location) + "</textarea>");
		}

		@Override
		final public SortedSet<Item> getChildren() {
			return set;
		}

		@Override
		final public String getDesc() {
			return head.toString();
		}
	}

	private class EditFile extends FileItem {
		public EditFile(final Context c, final File f) throws IOException {
			super(c, f, false);
			final Content item = ContentManager.getContent(context, location);
			head.append(HeadItem.extendedDesc(c, f));
			if (item instanceof Editable) {
				head.append(((Editable) item).process(null));
			}
		}

		final public String getDesc() {
			return head.toString();
		}
	}

	private class EditItem extends FileItem {
		private final File fi;

		public EditItem(final Context c, final File f) throws IOException {
			super(c, f, false);
			fi = f;
		}

		final public String getDesc() throws IOException {
			return standardDesc() + " <textarea name=\""
					+ TemplateUtil.escape(fi.getName()) + "\">"
					+ context.persistMan.getPlain(fi)
					+ "</textarea><br/>new name: <input name=\""
					+ TemplateUtil.escape(fi.getName()) + "_name\">";
		}
	}

	Context context;

	final StringBuffer foot = new StringBuffer();

	final StringBuffer head = new StringBuffer();

	private Item headItem;

	File location;

	public String actionCode() {
		return "edit";
	}

	public final String getFoot() {
		return foot.toString();
	}

	public final Item getHead() {
		return headItem;
	}

	public final String getRedirectTo() {
		return null;
	}

	public void init(final Context c, final Request req, final File l)
			throws IOException {
		location = l;
		context = c;
		head.append("<form method=\"post\" action=\""
				+ UrlUtil.getEncodedPath(c.config, location) + "\">");
		head.append("<input type=\"hidden\" name=\"actioncode\" value=\"editprocess\"/>");
		foot.append("<INPUT type=\"submit\" value=\"submit\"/>");
		foot.append("</form>");
		headItem = l.isDirectory() ? (Item) new EditDirectory(c, l, req)
				: new EditFile(c, l);
	}
}
